#include "stdafx.h"
#include "./animationhelper.h"
#include "./model.h"

AnimationHelper::AnimationHelper(Model* pModelMgr)
: m_pModelMgr(pModelMgr)
, m_pData(NULL)
{

}

AnimationHelper::~AnimationHelper()
{

}

void AnimationHelper::ChooseModel(string strModelName)
{
	m_pData = m_pModelMgr->GetModelByName(strModelName);
}

void AnimationHelper::TextureOffset(Point offsetPos)
{
	if (!m_pData)
	{
		return;
	}

	for (int i = 0; i < m_pData->nvertices; ++i)
	{
		m_pData->texcoords[i][0] += offsetPos.x;
		m_pData->texcoords[i][1] += offsetPos.y;
	}
}

void AnimationHelper::MultMatrix(float matrix[4][4])
{
	if (!m_pData)
	{
		return;
	}

	lib3ds_matrix_mult(m_pData->matrix, m_pData->matrix, matrix);
}

void AnimationHelper::TurnAngle(float radian)
{
	if (!m_pData)
	{
		return;
	}

	float rotate[4][4] = {
		cosf(radian), sinf(radian), 0., 0.,
		-sinf(radian), cosf(radian), 0., 0.,
		0., 0., 1., 0.,
		0., 0., 0., 1.
	};

	lib3ds_matrix_mult(m_pData->matrix, m_pData->matrix, rotate);
}

void AnimationHelper::SetAngle(float radian)
{
	if (!m_pData)
	{
		return;
	}

	float rotate[4][4] = {
		cosf(radian), sinf(radian), 0., 0.,
		-sinf(radian), cosf(radian), 0., 0.,
		0., 0., 1., 0.,
		0., 0., 0., 1.
	};

	memcpy(m_pData->matrix, rotate, sizeof(rotate));
}

void AnimationHelper::MoveOffset(float radian, float distance, Point* pos)
{
	if (!m_pData)
	{
		return;
	}

	m_pData->matrix[3][0] += distance * cosf(radian);
	m_pData->matrix[3][1] += distance * sinf(radian);

	if (pos)
	{
		pos->x = m_pData->matrix[3][0];
		pos->y = m_pData->matrix[3][1];
	}
}

void AnimationHelper::MoveToPos(Point pos)
{
	if (!m_pData)
	{
		return;
	}

	m_pData->matrix[3][0] = pos.x;
	m_pData->matrix[3][1] = pos.y;
}

void AnimationHelper::SetZPos(float pos)
{
	if (!m_pData)
	{
		return;
	}

	m_pData->matrix[3][2] = pos;
}

void AnimationHelper::SetColor(Color color)
{
	if (!m_pData)
	{
		return;
	}
	memcpy(&m_pData->color, &color, sizeof(Color));
}